package cn.itcast.mybatis.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCTest {
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
	
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			// 加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			// 获取连接
			String url = "jdbc:mysql://127.0.0.1:3306/mybatis-0111";
			String user = "root";
			String password = "root";
			conn = DriverManager.getConnection(url, user, password);
			// 获取statement，preparedStatement
			String sql = "select * from tb_user where id =?";
			ps = conn.prepareStatement(sql);
			// 设置参数
			ps.setLong(1, 1l);
			// 执行查询，获取结果集
			rs = ps.executeQuery();
			// 处理结果集
			while (rs.next()) {
				System.out.println(rs.getString("user_name"));
				System.out.println(rs.getString("name"));
				System.out.println(rs.getInt("age"));
			} 
		} finally {
			// TODO: handle exception
			if(rs!=null){
				rs.close();
			}
			if(ps!=null){
				ps.close();
			}
			if(conn!=null){
				conn.close();
			}
		}	 
		// 关闭连接，释放资源
	}
}
